# -*- coding: utf-8 -*-
import sys
import os
import re
import binascii
from scapy.all import Dot11
from scapy.all import sniff

aps = []
apshide = []
bssids = []
hideap = {}

W = '\033[0m'  # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[43m' # blue

USERID = os.getuid()
print(O + "USERID:" + str(USERID) + W)
if USERID is not 0:
    print(R + "WARNING: Must run as ROOT" + W)
    print(G + 'byebye!' + W)
    sys.exit(0)

iface = os.popen('ip address|grep -E "[a-z]+0:"|cut -d ":" -f 2')
ifaces = iface.read()
if "mon0" not in ifaces:
    openmon0 = os.popen('airmon-ng start wlan0')
    openmon0s = openmon0.read()
    openmon0.close()
iface.close()


def PacketHandler(pkt):
    if pkt.haslayer(Dot11):
        if pkt.type == 0 and pkt.subtype == 8:
            if pkt.addr2 not in bssids:
                print pkt
                bssids.append(pkt.addr2)
                cap = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
                print cap
                if re.search('privacy', cap):
                    if pkt.info:
                        a = 'privateState:Yes\t BSSID: %s\t SSID: %s' %\
                            (pkt.addr2, pkt.info)
                    else:
                        if binascii.hexlify(pkt.info)[:2] == "00":
                            print(R + "find hide wifi" + W)
                        a = 'privateState:Yes\t BSSID: %s\t SSID: %s' %\
                            (pkt.addr2, 'HIDEhide')
                else:
                    a = 'privateStatue:No\t BSSID: %s\t SSID: %s' %\
                        (pkt.addr2, pkt.info)

                with open('/tmp/test.txt', 'a+') as t:
                        t.write(a + '\n')
                print a

    # if len(bssids) > 5:
    #     sys.exit(0)


def GetApName(pkt):
    if pkt.haslayer(Dot11):
        if pkt.type == 0 and pkt.subtype == 4:
            if pkt.addr2 in apshide:
                cap = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
                print apshide
                print 'the hide ap name is: ' + B + pkt.info + W
                if re.search('privacy', cap):
                    print "has crypt\n"
                else:
                    print "has not crypt\n"

    # if pkt.haslayer(Dot11):
    #     if pkt.type == 0 and pkt.subtype == 8:
    #         if pkt.addr2 not in bssids:
    #             print pkt
    #             bssids.append(pkt.addr2)
    #             cap = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
    #             print cap
    #             if re.search('privacy', cap):
    #                 if pkt.info:
    #                     a = 'privateState:Yes\t BSSID: %s\t SSID: %s' %\
    #                         (pkt.addr2, pkt.info)
    #                 else:
    #                     if binascii.hexlify(pkt.info)[:2] == "00":
    #                         print(R + "find hide wifi" + W)
    #                     a = 'privateState:Yes\t BSSID: %s\t SSID: %s' %\
    #                         (pkt.addr2, 'HIDEhide')
    #             else:
    #                 a = 'privateStatue:No\t BSSID: %s\t SSID: %s' %\
    #                     (pkt.addr2, pkt.info)

    #             with open('/tmp/test.txt', 'a+') as t:
    #                     t.write(a + '\n')
    #             print a



if __name__ == "__main__":
    file1 = "/tmp/test.txt"
    if os.path.exists(file1):
        with open(file1) as f1:
            for line in f1:
                bssids.append(line.strip().split('\t')[-2].split(' ')[-1].strip())
                aps.append(line.strip().split('\t')[-1].split(' ')[-1].strip())
    print aps
    print bssids
    ap_bssid =  dict(zip(aps, bssids))
    for key in ap_bssid:
        print key, '\t', ap_bssid[key]

    if os.path.exists(file1):
        with open(file1) as f:
            for content in f:
                if 'HIDEhide' in content:
                    hideap.update({content.split('\t')[-2].split(' ')[-1].strip(): 'HIDEhide'})
                    print hideap

    ##sniff(count=80000, iface='mon0', prn=PacketHandler)
    sniff(iface="mon0", prn=GetApName)
